<template>
  <el-dialog @close="dkrzCancel('dkrzRuleForm')" :close-on-click-modal="false" title="提交对客日志"
    :visible.sync="dkrzVisible" width="600px" append-to-body>
    <div class="scroll-view">
      <el-form :model="dkrzRuleForm" :rules="dkrzRules" ref="dkrzRuleForm" label-position="top">
        <!-- log_file -->
        <el-form-item label="日志内容" prop="content">
          <el-input :rows="4" class="input-box" v-model="dkrzRuleForm.content" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="日志文件 (.jpg,.png,.gif)" prop="">
          <div class="file-box">
            <el-input style="position: absolute; z-index: 0; top: -999999px"
              v-model="dkrzRuleForm.log_file.length"></el-input>
            <el-upload :show-file-list="false" :auto-upload="false" action="#" class="avatar-uploader" accept="image/*"
              multiple :file-list="dkrzRuleForm.log_file" :on-change="(file, fileList) => {
                dkrzFileChange(file, fileList, 'log_file');
              }
                ">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <div class="img-box" v-for="(item, index) in dkrzRuleForm.log_file" :key="index">
              <div class="operation-box">
                <i class="iconfont icon-shanchu" @click.stop="dkrzFileRemove(index, 'log_file')"></i>
              </div>
              <img class="img-item" :src="item.show" />
            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer">
      <el-button @click="dkrzCancel('dkrzRuleForm')">关闭</el-button>
      <el-button type="primary" @click="dkrzSubmit('dkrzRuleForm')" :disabled="diaLoading" :loading="diaLoading">确
        定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import companyApi from "@/api/companyApi";
import adminApi from "@/api/adminApi";
import { getUserInfo } from "@/utils/auth";
import foremanApi from "@/api/foremanApi";

export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
  },
  data() {
    var dk_log_file = (rule, value, callback) => {
      if (!value.length) {
        callback(new Error("请上传日志文件"));
      } else {
        callback();
      }
    };
    return {
      dkrzVisible: false,
      dkrzRuleForm: {
        content: "",
        log_file: [],
      },
      dkrzRules: {
        log_file: [
          { required: true, validator: dk_log_file, trigger: "change" },
        ],
        content: [{ required: true, message: "请输入内容", trigger: "change" }],
      },
      diaLoading: false,
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      this.dkrzVisible = this.addDialogVisible;
    },
  },
  computed: {},
  mounted() { },
  methods: {
    // 对客日志
    dkrzCancel(formName) {
      this.dkrzRuleForm = {
        log_file: [],
        content: "",
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        this.$emit("onClose", true);
      });
    },
    dkrzSubmit(formName) {
      let dkrzRuleForm = this.dkrzRuleForm;
      console.log(dkrzRuleForm, "对客日志参数");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formdata = new FormData();
          formdata.append(
            "data",
            JSON.stringify({
              content: dkrzRuleForm.content,
              project_id: this.project_id,
            })
          );
          dkrzRuleForm.log_file.forEach((item) => {
            formdata.append("log_file[]", item.raw);
          });
          this.diaLoading = true;
          projectApi
            .addCustomerLogs(formdata)
            .then((res) => {
              console.log(res, "添加对客日志");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 对客日志选择文件
    dkrzFileChange(file, list, type) {
      console.log(file, list, type);
      list.forEach((item) => {
        item.show = URL.createObjectURL(item.raw);
      });
      this.dkrzRuleForm.log_file = list;
    },
    // 删除对客日志文件
    dkrzFileRemove(index) {
      this.dkrzRuleForm.log_file.splice(index, 1);
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>